package com.zhiwang.xiaoqu.ui.message

import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zhiwang.xiaoqu.R
import com.zhiwang.xiaoqu.di.annotation.Injectable
import com.zhiwang.xiaoqu.di.component.DaggerUserComponent
import com.zhiwang.xiaoqu.di.module.AutoDisposeModule
import com.zhiwang.xiaoqu.ktextension.setImmersive
import com.zhiwang.xiaoqu.mvp.model.response.Message
import com.zhiwang.xiaoqu.mvp.presenter.UserPresenter
import com.zhiwang.xiaoqu.ui.BaseListActivity
import java.util.*
import javax.inject.Inject

class MessageListActivity : BaseListActivity<Message, MessageListActivity.MessageViewHolder>(),Injectable {


    @Inject
    lateinit var userPresenter: UserPresenter

    var page = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        window.setImmersive(Color.TRANSPARENT, true)
        super.onCreate(savedInstanceState)
        startRefresh()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.mark_read, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == R.id.mark_read) {
            userPresenter.setMessageRead(::onError, ::onSetMessageReadSuccess)
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    private fun onSetMessageReadSuccess() {
        baseDatas.forEach {
            it.setHasRead()
        }
        baseListAdapter.notifyItemRangeChanged(0, baseListAdapter.itemCount)
    }

    override fun startLoadMore() {
        userPresenter.getMessageList(page + 1, ::onRefreshData, ::onLoadMoreData, ::onError)
    }

    override fun onLoadMoreComplete() {
        page++
    }

    override fun startRefresh() {
        userPresenter.getMessageList(1, ::onRefreshData, ::onLoadMoreData, ::onError)
    }

    override fun onRefreshComplete() {
        page = 1
    }

    override fun getEmptyLayoutRes() = R.layout.message_empty_layout

    override fun getToolBarTitle() = "我的消息"

    override fun getLayoutRes() = R.layout.item_my_message

    override fun whenBindViewHolder(holder: MessageViewHolder, item: Message?) {
        item?.apply {
            holder.dot.visibility = if (item.hasRead) View.GONE else View.VISIBLE
            holder.time.text = item.time
            holder.messageTextView.text = item.content
        }
    }

    override fun whenBindViewHolder(holder: MessageViewHolder, item: Message?, payloads: MutableList<Any>) {
        item?.apply {
            holder.dot.visibility = if (item.hasRead) View.GONE else View.VISIBLE
            holder.time.text = item.time
            holder.messageTextView.text = item.content
        }
    }

    class MessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val messageTextView: TextView = itemView.findViewById(R.id.tv_message)
        val sender: TextView = itemView.findViewById(R.id.tv_sender)
        val time: TextView = itemView.findViewById(R.id.time)
        val dot = itemView.findViewById<View>(R.id.message_dot)
    }
}
